/*
 * @Author: Turump 
 * @Date: 2021-03-30 11:10:52 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-03-30 11:12:42
 */


const { ccclass, disallowMultiple, menu, executionOrder } = cc._decorator;

@ccclass
@disallowMultiple()
@menu("屏幕适配/Canvas适配")

/** !#zh: 用于屏幕适配,挂载到Canvas上，一个场景只需要一个 */
export default class ViewSizeAdapter extends cc.Component {

    setup() {
        let frameSize = cc.view.getFrameSize();
        let designSize = cc.Canvas.instance.designResolution;
        let scaleX = frameSize.width / designSize.width;
        let scaleY = frameSize.height / designSize.height;
        let minScale: number;
        if (scaleX > scaleY) {
            minScale = scaleY;
        } else {
            minScale = scaleX;
        }
        // cc.log(`ViewSizeAdapter :frameSize:${frameSize.toString()}, winSize:${cc.winSize.toString()}`);
        // cc.log("ViewSizeAdapter scaleX == " + scaleX + " y ==" + scaleY);
        // cc.log("ViewSizeAdapter minScale == " + minScale);
        // cc.log("ViewSizeAdapter Rx == " + frameSize.width / minScale + " Ry ==" + frameSize.height / minScale);
        cc.view.setDesignResolutionSize(frameSize.width / minScale, frameSize.height / minScale, cc.ResolutionPolicy.NO_BORDER);
        (cc.Canvas.instance as any).alignWithScreen = true;
    }

    onLoad() {
        this.setup();
        cc.view.setResizeCallback(() => {
            this.setup();
            cc.director.dispatchEvent(new cc.Event.EventCustom("ResizeFrame", true));
        });
    }
}